﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace _90数组_一维数组
{
    internal class Program
    {
        //定义数组和访问数组元素
        private static void Main1(string[] args)
        {
            // int[] array;//定义数组变量
            // array = new int[3];//创建数组对象并复制给array
            // array[0] = 8;//给第0个元素赋值
            // array[1] = 9;
            // array[2] = 6;
            // Console.WriteLine(array[2]);//访问第2个元素

            //string[] names = new string[3];
            //names[0] = "张三";
            //names[1] = "李四";
            //names[2] = "王五";
            //Console.WriteLine(names[1]);

            // string[] names = new string[3] { "张三", "李四", "王五" };
            // string[] names = new string[] { "张三", "李四", "王五" };

            // string[] names = { "张三", "李四", "王五" };
            // Console.WriteLine(names[1]);

            float[] array = { 2.3f, 1.2f, 2.8f };
            Console.WriteLine(array[2]);//2.3f
        }

        //数组的遍历  案例求元素累加和
        private static void Main2(string[] args)
        {
            int[] array = { 2, 3, 4, 7, 8, 9, 5 };

            int sum = 0;
            for (int i = 0; i < array.Length; i++)
            {
                sum += array[i];
            }
            Console.WriteLine(sum);
        }
        //练习求数组的最大值
        private static void Main3(string[] args)
        {
            int[] array = { 2, 3, 4, 7, 8, 9, 5 };
            int max = array[0];//先假设第0个元素是最大的
            for (int i = 1; i < array.Length; i++)//把剩下的元素都和max比较一下
            {
                //如果当前元素比max大,把max换成当前元素
                if (array[i] > max)
                {
                    max = array[i];
                }
            }
            Console.WriteLine(max);
        }
        //练习:求数组最小值
        private static void Main4(string[] args)
        {
            int[] array = { 2, 3, 4, 7, 8, 9, 5 };
            int min = array[0];
            for (int i = 1; i < array.Length; i++)
            {
                if (array[i] < min)
                {
                    min = array[i];
                }
            }
            Console.WriteLine(min);
        }

        //练习: 输入5个数字,输入完成后在一行内输出所有元素,再输出总和
        private static void Main5(string[] args)
        {
            double[] array = new double[5];//创建一个数组用来存储输入的数字
            for (int i = 0; i < array.Length; i++)
            {
                Console.WriteLine("请输入第{0}个数字", i + 1);
                array[i] = double.Parse(Console.ReadLine());
            }

            //输出到一行
            int sum = 0;
            for (int i = 0; i < array.Length; i++)
            {
                Console.Write(array[i]);
                if (i < array.Length - 1)
                {
                    Console.Write(",");
                }
                sum += i;
            }
            Console.WriteLine("总和是:" + sum);
        }
        private static void Main(string[] args)
        {
            int[] array = { 2, 3, 4, 7, 8, 9, 5 };
            foreach (var item in array)
            {
                if (item % 2 == 0)
                {
                    Console.WriteLine(item);
                }
            }

            //注意:item不能赋值
            // IEnumerator ien = array.GetEnumerator();
            // while (ien.MoveNext())
            // {
            //     Console.WriteLine(ien.Current); //current属性是只读的
            // }
        }
    }
}